library(foreign) ## version: foreign_0.8-66
## Replication code for Estimating Candidates’ Political Orientation in a Polarized Congress
## We use R for 3.2.4 GUI 1.67 Mavericks build (7152), running on Mac OS 10.12.1 

library(ggplot2) ## We use version: ggplot2_2.1.0
library(plyr)    ## We use version: plyr_1.8.4
library(dplyr)   ## We use version: dplyr_0.5.0
library(doBy)    ## We use version: doBy_4.5-14
library(xtable)  ## We use version: xtable_1.8-2

load(file=paste("candidatepositions_house_analysis.RData",sep=""))



##########
## Effect of political orientation on elections
##########
#reg1<-(lm(gen.elect.pct~scale(dwnom1_contemporaneous)+scale(-district.pres.vs), data=analysis_all[analysis_all$election>1998 & analysis_all$Party=="100",] ))

analysis_all$district.pres.vs <- as.numeric(as.character(analysis_all$district.pres.vs))
analysis_all$gen.elect.pct <- as.numeric(as.character(analysis_all$gen.elect.pct))

reg2<-(lm(gen.elect.pct~scale(cfscores.dyn)+scale(-district.pres.vs)+factor(election), data=analysis_all[analysis_all$election>"1998" & analysis_all$Party=="100",] ))

reg3<-(lm(gen.elect.pct ~scale(npat_score)+scale(-district.pres.vs)+factor(election), data=analysis_all[analysis_all$election>1998 & analysis_all$Party=="100",] ))

reg4<-(lm(gen.elect.pct ~scale(twitter_idealpoint) +scale(-district.pres.vs), data=analysis_all[analysis_all$election>1998 & analysis_all$Party=="100",] ))

reg5<-(lm(gen.elect.pct  ~scale(am_estimate)+scale(-district.pres.vs), data=analysis_all[analysis_all$election>1998 & analysis_all$Party=="100",] ))

reg6<-(lm(gen.elect.pct  ~scale(js_ideology_contemporaneous)+scale(-district.pres.vs), data=analysis_all[analysis_all$election>1998 & analysis_all$Party=="100",] ))

#reg7<-(lm(gen.elect.pct~scale(dwnom1_contemporaneous)+scale(-district.pres.vs), data=analysis_all[analysis_all$election>1998  & analysis_all$Party=="200",] ))

reg8<-(lm(gen.elect.pct~scale(cfscores.dyn )+scale(-district.pres.vs), data=analysis_all[analysis_all$election>1998 & analysis_all$Party=="200",] ))

reg9<-(lm(gen.elect.pct ~scale(npat_score )+scale(-district.pres.vs), data=analysis_all[analysis_all$election>1998 & analysis_all$Party=="200",] ))

reg10<-(lm(gen.elect.pct ~scale(twitter_idealpoint) +scale(-district.pres.vs), data=analysis_all[analysis_all$election>1998 & analysis_all$Party=="200",] ))

reg11<-(lm(gen.elect.pct  ~scale(am_estimate)+scale(-district.pres.vs), data=analysis_all[analysis_all$election>1998 & analysis_all$Party=="200",] ))

reg12<-(lm(gen.elect.pct  ~scale(js_ideology_contemporaneous)+scale(-district.pres.vs), data=analysis_all[analysis_all$election>1998 & analysis_all$Party=="200",] ))

library(stargazer)
## Table 8: Relationship between political orientation and election results for Democratic candidates.
cat(print(stargazer(reg2, reg3, reg4, reg5, reg6)),file="Table8.txt")
## Table 9: Relationship between political orientation and election results for Republican candidates.
cat(print(stargazer( reg8,reg9, reg10, reg11, reg12)),file="Table9.txt")
